﻿using MediatR;
using Newtonsoft.Json;
using System.Net.Http.Json;
using System.Text.Json.Serialization;
using Traceability.Domain;
using Traceability.ErrorCode;
using Traceability.Infrastructure;

namespace Traceability.App.Applications.Traceability
{
    public class TraceabilityRemoveCommandHandler : IRequestHandler<TraceabilityRemoveCommand, APIResult<int>>
    {
        private readonly IBaseRepository<T_Traceability> tranceRepository;

        public TraceabilityRemoveCommandHandler(IBaseRepository<T_Traceability> tranceRepository)
        {
            this.tranceRepository = tranceRepository;
        }

        public Task<APIResult<int>> Handle(TraceabilityRemoveCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            result.Code = APIEnum.ok;
            var list = JsonConvert.DeserializeObject<List<T_Traceability>>(request.Json);
            foreach (var item in list)
            {
                item.IsDelete = true;
            }
            tranceRepository.UpdateRange(list);
            result.Message = "删除成功";
            result.Data = 1;
            return Task.FromResult(result);
        }
    }
}
